/*******************************************************************
 *   > File Name: led.c
 *   > Author: fly
 *   > Mail: 1358326274@qq.com
 *   > Create Time: 2020年07月21日 星期二 22时31分37秒
 ******************************************************************/
/*=================================================================
 * C语言点亮led灯：对应GPIO口输出低电平，点亮LED
 * D22->GPJ0_3
 * D23->GPJ0_4
 * D24->GPJ0_5
 * D25->PWMOUT1/GPD0_1
 *================================================================*/
#include "led.h"

#define GPD0CON         0xE02000A0
#define GPD0DAT         0xE02000A4
#define GPD0PUD         0xE02000A8

#define GPJ0CON         0xE0200240
#define GPJ0DAT         0xE0200244
#define GPJ0PUD         0xE0200248

#define rGPD0CON        (*(volatile unsigned int *)GPD0CON)
#define rGPD0DAT        (*(volatile unsigned int *)GPD0DAT)
#define rGPD0PUD        (*(volatile unsigned int *)GPD0PUD)

#define rGPJ0CON        (*(volatile unsigned int *)GPJ0CON)
#define rGPJ0DAT        (*(volatile unsigned int *)GPJ0DAT)
#define rGPJ0PUD        (*(volatile unsigned int *)GPJ0PUD)

void delay(unsigned long count)
{
    volatile unsigned long i = count;
    while(i--)
        ;
}

void led_init(void)
{
    rGPJ0CON = 0x00111000;
    rGPD0CON = 0x00000010;

    rGPJ0DAT = ((0x1<<3) | (0x1<<4) | (0x1<<5));
    rGPD0DAT = (0x1<<1);
}

//0:off, 1:on
void led_set(unsigned int id, unsigned int value)
{

    if (id == 0)//led0
    {
        if(value){
            rGPJ0DAT &= ~(0x1<<3);
        }else{
            rGPJ0DAT |= 0x1<<3;
        }
    }
    else if (id == 1)//led1
    {
        if(value){
            rGPJ0DAT &= ~(0x1<<4);
        }else{
            rGPJ0DAT |= 0x1<<4;
        }
    }
    else if (id == 2)//led2
    {
        if(value){
            rGPJ0DAT &= ~(0x1<<5);
        }else{
            rGPJ0DAT |= 0x1<<5;
        }
    }
    else if (id == 3)//led4
    {
        if(value){
            rGPD0DAT &= ~(0x1<<1);
        }else{
            rGPD0DAT |= 0x1<<1;
        }
    
    }

#if 0
    switch(id)
    {
        case 0:
            if(value){
                rGPJ0DAT &= ~(0x1<<3); 
            }else{
                rGPJ0DAT |= 0x1<<3;
            }
            break;
        case 1:
            if(value){
                rGPJ0DAT &= ~(0x1<<4);
            }else{
                rGPJ0DAT |= 0x1<<4;
            }
            break;
        case 2:
            if(value){
                rGPJ0DAT &= ~(0x1<<5);
            }else{
                rGPJ0DAT |= 0x1<<4;
            }
            break;
        case 3:
            if(value){
                rGPD0DAT &= ~(0x1<<1);
            }else{
                rGPD0DAT |= 0x1<<1;
            }
            break;
        default:
            break;
    }
#endif
}

void led_test(void)
{
    led_init();

    while(1)
    {
#if 0
        // led亮
        rGPJ0DAT = ((0<<3) | (0<<4) | (0<<5));
        rGPD0DAT = (0<<1);
        // 延时
        delay(900000);
        // led灭
        rGPJ0DAT = ((1<<3) | (1<<4) | (1<<5));
        rGPD0DAT = (1<<1);
        // 延时
        delay(900000);
#else
        led_set(0, 1);
        delay(900000);
        led_set(0, 0);
        led_set(1, 1);
        delay(900000);
        led_set(1, 0);
        led_set(2, 1);
        delay(900000);
        led_set(2, 0);
        led_set(3, 1);
        delay(900000);
        led_set(3, 0);
#endif
    }   
}

void led_breath(void)
{
    volatile unsigned int PWM = 1;
    volatile unsigned int CYCLE = 2500;

    led_init();

    while(1)
    {
        led_set(0, 0);
        delay(450000);

        for(PWM=1;PWM<CYCLE;PWM++)
        {
            led_set(0,1);
            delay(PWM);
            led_set(0,0);
            delay(CYCLE-PWM);
        }

        led_set(0,1);
        delay(1000);

        for(PWM=1;PWM<CYCLE;PWM++)
        {   
            led_set(0,0);
            delay(PWM);
            led_set(0,1);
            delay(CYCLE-PWM);
        }

        led_set(0,0);
    }
}
